package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SpecialCargoApplyErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SuperWhitelistOogConvert;
import com.cmc.cloud.cmclink.doc.entity.SuperWhitelistOogDO;
import com.cmc.cloud.cmclink.doc.mapper.SuperWhitelistOogMapper;
import com.cmc.cloud.cmclink.doc.service.SuperWhitelistOogService;
import com.cmc.cloud.cmclink.doc.vo.superwhitelistvo.*;
import com.cmc.cloud.cmclink.system.api.api.file.FileApi;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;


/**
 * 有条件接载清单(OOG) Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class SuperWhitelistOogServiceImpl implements SuperWhitelistOogService {

    @Resource
    private SuperWhitelistOogMapper superWhitelistOogMapper;

    @Resource
    private FileApi fileApi;

    @Override
    public void createSuperWhitelistOog(SuperWhitelistOogBaseVO createReqVO) {
        // 插入
        LambdaQueryWrapperX<SuperWhitelistOogDO> queryWrapperX = new LambdaQueryWrapperX<SuperWhitelistOogDO>()
                .eqIfPresent(SuperWhitelistOogDO::getSpecialCargoType, createReqVO.getSpecialCargoType())
                .eqIfPresent(SuperWhitelistOogDO::getPraNo, createReqVO.getPraNo());
        List<SuperWhitelistOogDO> whitelistOogDos = superWhitelistOogMapper.selectList(queryWrapperX);
        if (CollectionUtil.isNotEmpty(whitelistOogDos)) {
            throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.SUPER_WHITELIST_OOG_ERROR);
        }
        SuperWhitelistOogDO superWhitelistOog = SuperWhitelistOogConvert.INSTANCE.convert(createReqVO);
        superWhitelistOogMapper.insert(superWhitelistOog);
    }

    @Override
    public void updateSuperWhitelistOog(SuperWhitelistOogUpdateReqVO updateReqVO) {
        // 更新
        SuperWhitelistOogDO updateObj = SuperWhitelistOogConvert.INSTANCE.convert(updateReqVO);
        superWhitelistOogMapper.updateById(updateObj);
    }

    @Override
    public void deleteSuperWhitelistOog(Long id) {
        // 删除
        superWhitelistOogMapper.deleteById(id);
    }

    @Override
    public SuperWhitelistOogDO getSuperWhitelistOog(Long id) {
        return superWhitelistOogMapper.selectById(id);
    }

    @Override
    public List<SuperWhitelistOogDO> getSuperWhitelistOogList(Collection<Long> ids) {
        return superWhitelistOogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SuperWhitelistOogDO> getSuperWhitelistOogPage(SuperWhitelistOogPageReqVO reqVO) {
        LambdaQueryWrapperX<SuperWhitelistOogDO> queryWrapperX = new LambdaQueryWrapperX<SuperWhitelistOogDO>()
                .eqIfPresent(SuperWhitelistOogDO::getSpecialCargoType, reqVO.getSpecialCargoType())
                .eqIfPresent(SuperWhitelistOogDO::getPraNo, reqVO.getPraNo())
                .likeIfPresent(SuperWhitelistOogDO::getPors, reqVO.getPors())
                .likeIfPresent(SuperWhitelistOogDO::getPols, reqVO.getPols())
                .likeIfPresent(SuperWhitelistOogDO::getPods, reqVO.getPods())
                .likeIfPresent(SuperWhitelistOogDO::getDels, reqVO.getDels());
        return superWhitelistOogMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public List<SuperWhitelistOogDO> getExportList(SuperWhitelistReqVO reqVO) {
        LambdaQueryWrapperX<SuperWhitelistOogDO> dgQuery = new LambdaQueryWrapperX<SuperWhitelistOogDO>()
                .eqIfPresent(SuperWhitelistOogDO::getSpecialCargoType, reqVO.getSpecialCargoType())
                .likeIfPresent(SuperWhitelistOogDO::getPraNo, reqVO.getPraNo())
                .likeIfPresent(SuperWhitelistOogDO::getPors, reqVO.getPors())
                .likeIfPresent(SuperWhitelistOogDO::getPols, reqVO.getPols())
                .likeIfPresent(SuperWhitelistOogDO::getPods, reqVO.getPods())
                .likeIfPresent(SuperWhitelistOogDO::getDels, reqVO.getDels());
        return superWhitelistOogMapper.selectList(dgQuery);
    }

    @Override
    public void copyFromHistory(SpecialWhiteListCopyReqVO reqVO) {
        //复制源主数据
        SuperWhitelistOogDO originalWhiteOogInfo = superWhitelistOogMapper.selectById(reqVO.getOriginalId());
        if (null == originalWhiteOogInfo) {
            throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.SPECIAL_CARGO_APPLY_NOT_EXISTS);
        }
        SuperWhitelistOogDO newApply = new SuperWhitelistOogDO();
        BeanUtil.copyProperties(originalWhiteOogInfo, newApply, "createTime", "updateTime", "creator", "updater");
        newApply.setId(null);
        superWhitelistOogMapper.insert(newApply);
    }

    @Override
    @Lock4j(name = "oogDeleteFile", keys = {"#id"})
    public void deleteFile(Long id) {
        SuperWhitelistOogDO superWhitelistOogDO = superWhitelistOogMapper.selectById(id);
        if (superWhitelistOogDO == null) {
            throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.SPECIAL_CARGO_APPLY_NOT_EXISTS);
        }
        if (superWhitelistOogDO.getFileId() == null) {
            throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.SPECIAL_CARGO_CNTR_FILE_NOT_EXISTS);
        }
        CommonResult<Boolean> result = fileApi.delete(superWhitelistOogDO.getFileId());
        if (result.isError()) {
            throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.SPECIAL_CARGO_CNTR_FILE_DELETE_ERROR);
        }
        superWhitelistOogDO.setFileId(null);
        superWhitelistOogMapper.updateById(superWhitelistOogDO);

    }
}
